//
//  ViewController.m
//  UI_20_XML_SAXAndDOM
//
//  Created by li cheng on 16/8/10.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "ViewController.h"
#import "Student.h"
#import "GDataXMLNode.h"
#import "XMLReader.h"
@interface ViewController ()<NSXMLParserDelegate>
@property (nonatomic, strong) NSMutableArray *stuArray;
@property (nonatomic, strong) NSString *currentElementName;
@property (nonatomic, strong) NSMutableArray *domStuArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    // 将XML结构, 转换成Dictionary.
    NSString *xmlPath = [[NSBundle mainBundle] pathForResource:@"stu_xml" ofType:@"xml"];
    NSData *data = [NSData dataWithContentsOfFile:xmlPath];
    NSError *error = nil;
    NSDictionary *dic = [XMLReader dictionaryForXMLData:data error:&error];
    NSLog(@"%@", dic);
    
}
// XML 分为两种解析方式,
// 第一种 SAX 逐行解析 -- 使系统的解析器 XMLParser
- (IBAction)SAXParse:(id)sender {
    NSString *xmlPath = [[NSBundle mainBundle] pathForResource:@"stu_xml" ofType:@"xml"];
    NSData *xmlData = [NSData dataWithContentsOfFile:xmlPath];
    NSXMLParser *xmlPraser = [[NSXMLParser alloc] initWithData:xmlData];
    xmlPraser.delegate = self;
    // 必须的执行开始的方法 parse(解析)
    [xmlPraser parse];
}
#pragma mark - 解析器开始
- (void)parserDidStartDocument:(NSXMLParser *)parser{
    self.stuArray = [NSMutableArray array];
}
#pragma mark - 遇到节点头 方法的回调, 记录当前节点,方便后续赋值
- (void)parser:(NSXMLParser *)parser didStartElement:(nonnull NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(nonnull NSDictionary<NSString *,NSString *> *)attributeDict{
    _currentElementName = elementName;
    NSLog(@"节点头:%@", elementName);
    if ([elementName isEqualToString:@"student"]) {
        Student *student = [Student new];
        [_stuArray addObject:student];
    }
}
#pragma mark - 节点的值, 根据节点决定值, 没有值时对应\n\t \n\t\t
- (void)parser:(NSXMLParser *)parser foundCharacters:(nonnull NSString *)string{
    // string 是\n \t
//    if (![string hasSuffix:@"  "] && ![string hasSuffix:@"\n"]) {
    if (_currentElementName) {
        Student *student = [_stuArray lastObject];
        [student setValue:string forKey:_currentElementName];
        NSLog(@"节点值:%@-----结束", string);
    }
    
//    }
}
#pragma mark - 遇到节点尾 方法的回调 当前节点结束
- (void)parser:(NSXMLParser *)parser didEndElement:(nonnull NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName{
    NSLog(@"节点尾:%@", elementName);
    _currentElementName = nil;
}
#pragma mark - 解析器结束
- (void)parserDidEndDocument:(NSXMLParser *)parser{
    NSLog(@"-----%@", _stuArray);
}

#pragma mark - 接媳妇失败
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{
    NSLog(@"%@", parseError);
}

// 第二种解析方式, DOM解析
// 关于GDdataXMLNode的引入
// 1.引入.h与.m文件
// 2.引入系统动态库libxml2.tbd
// 3.根据GDataXMLNode文件中的提示, 添加  /usr/include/libxml2
// 4.如果是ARC环境, 需对该文件进行MRC编译, 添加 -fon-bojc-arc
#pragma mark - 第二种解析方式, DOM解析
- (IBAction)DOXParse:(id)sender {
    _domStuArray = [NSMutableArray array];
    NSString *xmlPath = [[NSBundle mainBundle] pathForResource:@"stu_xml" ofType:@"xml"];
    NSData *data = [NSData dataWithContentsOfFile:xmlPath];
    NSError *error = nil;
    //创建GDataXMLDocument对象
    
    //根据XMl的data数据生成一个树形结构 Document
    GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:data options:0 error:&error];
    // 2.根据document获取根节点
    GDataXMLElement *rootElement = [document rootElement];
    // 3.根据根节点获取子节点数组
    NSArray *rootChildElements = rootElement.children;
    NSLog(@"---------%@",rootElement.name);
    // 4.遍历子节点数组
    for (GDataXMLElement *childElement in rootChildElements) {
        NSLog(@"+++++++childElement name:%@",childElement.name);
        // 5对应所有student节点
        Student *stu = [Student new];
        [self.domStuArray addObject:stu];
        // 6.遍历student 节点的所有节点
        for (GDataXMLElement *grandSonElement in childElement.children) {
            //            NSLog(@"grandSonElement name :%@",grandSonElement.stringValue);
            // 7.赋值
            [stu setValue:grandSonElement.stringValue forKey:grandSonElement.name];
        }
    }
    NSLog(@"%@",self.domStuArray);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
